package com.spcloud.demo.zuul.route;

import com.netflix.zuul.context.RequestContext;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CustomSimpleRouteLocator extends SimpleRouteLocator
        implements RefreshableRouteLocator {

    public CustomSimpleRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
    }

    @Override
    protected LinkedHashMap<String, ZuulProperties.ZuulRoute> locateRoutes() {

        ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute("baidu", "/baidu/**", "baidu", "http://www.baidu.com", false,
                false, null) ;
        LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap<String, ZuulProperties.ZuulRoute>();
        routesMap.putAll(super.locateRoutes());
        routesMap.put(route.getPath(), route);
        return routesMap;
    }

    @Override
    public List<Route> getRoutes() {
        List<Route> list = new ArrayList<>();
        Route route = new Route("baidu", "/baidu/**", "http://www.baidu.com", "",
                false, null) ;
        list.add(route);
        System.out.println("load all routes");
        return list;
    }
//    @Override
//    public Route getMatchingRoute(final String path) {
//        RequestContext currentContext = RequestContext.getCurrentContext();
//        HttpServletRequest request = currentContext.getRequest();
//        System.out.println("path:" + path);
//        Route route = new Route("baidu", "/baidu/**", "http://www.baidu.com", "",
//                false, null) ;
//        return route;
//    }

    @Override
    public void refresh() {

    }


}
